package com.sojson.project.sys.config.service.impl;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sojson.enums.core.EConfigType;
import com.sojson.enums.core.EGeneralYesNo;
import com.sojson.project.sys.config.dao.ConfigDao;
import com.sojson.project.sys.config.entity.dto.ConfigDto;
import com.sojson.project.sys.config.entity.dto.ConfigPageDto;
import com.sojson.project.sys.config.entity.po.Config;
import com.sojson.project.sys.config.entity.vo.ConfigVo;
import com.sojson.project.sys.config.service.ConfigService;
import com.sojson.util.ExpUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.SystemParamUtil;
import com.sojson.util.file.constant.ConstantFileFieldName;
import com.sojson.util.file.depot.FileDepotUtil;
import com.sojson.util.file.enums.EFileTableBase;

/**
 * 配置表Service实现类
 * 
 * @author liu
 * @date 2022-08-26
 */
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigDao, Config> implements ConfigService {

    @Lazy
    @Autowired
    private ConfigService configService;
    @Resource
    private ConfigDao configDao;

    /**
     * 单条插入
     * 
     * @param dto
     * @return
     * @throws IOException 
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(Config dto) throws IOException {
        // 去重
        LambdaQueryWrapper<Config> query = Wrappers.lambdaQuery(Config.class);
        query.eq(Config::getKey, dto.getKey());
        int count = configService.count(query);
        if (count > 0) {
            ExpUtil.throwEx("别名已存在!");
        }

        Integer type = dto.getType();
        if ((type == EConfigType.IMG.getCode() || type == EConfigType.VIDEO.getCode()
            || type == EConfigType.FILE.getCode()) && dto.getIsInner() == EGeneralYesNo.YES.getCode()) {
            FileDepotUtil.insertListSync(EFileTableBase.CONFIG.getCode(), ConstantFileFieldName.VAL, dto.getVal());
        }

        boolean save = configService.save(dto);
        // 更新缓存
        if (save) {
            SystemParamUtil.put(dto.getKey(), configService.findByKey(dto.getKey(), false));
        }
        return save;
    }

    /**
     * 通过id更新
     * 
     * @param dto
     * @return
     * @throws IOException 
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Config dto) throws IOException {
        Integer type = dto.getType();
        if ((type == EConfigType.IMG.getCode() || type == EConfigType.VIDEO.getCode()
            || type == EConfigType.FILE.getCode()) && dto.getIsInner() == EGeneralYesNo.YES.getCode()) {
            Config getById = configService.getById(dto.getId());
            FileDepotUtil.updateListSync(EFileTableBase.CONFIG.getCode(), ConstantFileFieldName.VAL, dto.getVal(),
                getById.getVal());
        }

        boolean updateById = configService.updateById(dto);

        if (updateById) {
            ConfigVo findById = configService.findById(dto.getId());
            SystemParamUtil.put(findById.getKey(), findById);
        }
        return updateById;
    }

    /**
     * 列表查询
     * 
     * @param dto
     * @return
     */
    @Override
    public List<ConfigVo> list(ConfigDto dto) {
        List<ConfigVo> list = configDao.list(dto);
        return list;
    }

    /**
     * 分页查询
     * 
     * @param dto
     * @return
     */
    @Override
    public List<ConfigVo> page(ConfigPageDto dto) {
        List<ConfigVo> page = configDao.page(dto);
        return page;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @Override
    public ConfigVo findById(String id) {
        return configDao.findById(id);
    }

    /**
     * 通过key查询
     * 
     * @param key
     * @return
     * @throws IOException 
     */
    @Override
    public ConfigVo findByKey(String key) throws IOException {
        return findByKey(key, true);
    }

    /**
     * 通过key查询
     * 
     * @param key
     * @param isVisit   是否允许外界访问
     * @return
     * @throws IOException 
     */
    @Override
    public ConfigVo findByKey(String key, boolean isVisit) throws IOException {
        ConfigVo config = SystemParamUtil.get(key);

        if (config != null && StringUtil.isNotBlank(config.getVal())) {
            if (isVisit) {
                return config;
            } else {
                if (config.getIsVisit() == EGeneralYesNo.YES.getCode()) {
                    return config;
                }
            }
        }

        return configDao.findByKey(key, isVisit);
    }

    /**
     * 通过对象删除
     * 
     * @param dto
     * @return
     */
    @Override
    public boolean removeByDto(ConfigDto dto) {
        configDao.removeByDto(dto);
        return true;
    }

    /**
     * 通过id列表删除
     * 
     * @param ids
     * @return
     * @throws IOException 
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(List<String> ids) throws IOException {
        List<String> findKeyByIds = configDao.findKeyByIds(ids);

        int deleteBatchIds = configDao.deleteBatchIds(ids);

        if (deleteBatchIds > 0) {
            SystemParamUtil.del(findKeyByIds.toArray(new String[] {}));
        }
        return true;
    }

    /**
     * 清空数据
     * 
     * @return
     */
    @Override
    public int clear() {
        return configDao.clear();
    }

}